package kr.pe.johnkim.chattingday.mobile.rest.controller;

import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayLog;
import kr.pe.johnkim.chattingday.mobile.rest.service.ChattingDayLogService;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Chatting Day 로그 관련 Controller 클래스 이다.
 *
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 5. 16
 */

@Controller
@Path("/chattingDayLogController")
public class ChattingDayLogController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(ChattingDayLogController.class);

    @Autowired
    private ChattingDayLogService chattingDayLogService = null;

    private String sessionInterval;
    private String passwordActivateInterval;
    private String mailSenderUsername;

    /**
     * 로그를 저장 한다.
     * 
     * @param is
     * @throws Exception
     */
    @POST
    @Path("/registerLog")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public void registerLog(InputStream is) throws Exception {
        logger.info("registerLog");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonLog = new JSONObject(line);
        JSONObject jsonLog2 = jsonLog.getJSONObject("chattingDayLog");

        ChattingDayLog parameterLog = new ChattingDayLog();

        parameterLog.setId(jsonLog2.getString("id"));
        parameterLog.setViewName(jsonLog2.getString("viewName"));
        parameterLog.setAccessDate(jsonLog2.getString("accessDate"));

        logger.info("id         : {}", parameterLog.getId());
        logger.info("viewName   : {}", parameterLog.getViewName());
        logger.info("accessDate : {}", parameterLog.getAccessDate());

        chattingDayLogService.registerLog(parameterLog);
    }

    /**
     * 자바 클래스에서 프로퍼티 설정 값 로딩하기 예제용 테스트.
     *
     * 1. class 루트에 위치하는 log4j.properties 의 설정값 가져 오기.
     * 2. servlet.xml 파일에 정의한 프로퍼티 파일의 설정값 가져 오기.
     * 
     * @throws Exception
     */
    @POST
    @Path("/testProperties")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public void testProperties() throws Exception {
        logger.info("testProperties");

        Properties properties = new Properties();

        InputStream in = this.getClass().getClassLoader().getResourceAsStream("/log4j.properties");

        properties.load(in);

        logger.info("log4j.appender.rolling.MaxFileSize : {}", properties.getProperty("log4j.appender.rolling.MaxFileSize"));

        logger.info("sessionInterval          : {}", this.getSessionInterval());
        logger.info("passwordActivateInterval : {}", this.getPasswordActivateInterval());
        logger.info("mailSenderUsername       : {}", this.getMailSenderUsername());

    }

    /**
     * 자바 클래스에서 프로퍼티 설정 값 로딩하기 예제에서 사용하는 setter 와 getter 정의.
     *
     * sessionInterval, passwordActivateInterval
     *
     */

    public String getSessionInterval() {
        return sessionInterval;
    }

    public void setSessionInterval(String sessionInterval) {
        this.sessionInterval = sessionInterval;
    }

    public String getPasswordActivateInterval() {
        return passwordActivateInterval;
    }

    public void setPasswordActivateInterval(String passwordActivateInterval) {
        this.passwordActivateInterval = passwordActivateInterval;
    }

    public String getMailSenderUsername() {
        return mailSenderUsername;
    }

    public void setMailSenderUsername(String mailSenderUsername) {
        this.mailSenderUsername = mailSenderUsername;
    }
}
